package com.example.final_project.Usr.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.final_project.MainView;
import com.example.final_project.R;

public class AppSetActivity extends AppCompatActivity {

    private Button logout_BT;
    private Context context;
    private ImageView set_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_set);

        context = getBaseContext();

        //设置注销事件
        logout_BT = this.findViewById(R.id.logout_BT);
        logout_BT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
                if(sp != null && !sp.getString("name","").equals("")){
                    SharedPreferences.Editor editor = sp.edit();
                    editor.remove("name");
                    editor.remove("age");
                    editor.remove("sex");
                    editor.remove("phone");
                    editor.remove("address");
                    editor.commit();
                    Intent intent = new Intent(AppSetActivity.this, MainView.class);
                    intent.setAction("backToUserFrag");
                    startActivity(intent);
                }else{
                    Toast.makeText(context,"您还没有登录账户",Toast.LENGTH_SHORT).show();
                }
            }
        });

        //设置个人信息界面
        RelativeLayout user_info = this.findViewById(R.id.user_info_set);
        user_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp = AppSetActivity.this.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
                if(sp != null && !sp.getString("name","").equals("")){
                    //登录后，跳转到个人信息界面
                    Intent intent = new Intent(AppSetActivity.this,UserInfoActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(AppSetActivity.this,"小橙子检测到您还没有登录,请登录后查看",Toast.LENGTH_SHORT).show();
                    //没有登录跳转到登录界面
                    Intent intent = new Intent(AppSetActivity.this,LoginActivity.class);
                    startActivity(intent);
                }

            }
        });

        //设置返回userfragment
        set_back = this.findViewById(R.id.set_back);
        set_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AppSetActivity.this,MainView.class);
                intent.setAction("backToUserFrag");
                startActivity(intent);
            }
        });

    }
}